package com.kai.service.system.controller;

import com.kai.service.data.Result;
import com.kai.service.data.request.system.CreateRoleRequest;
import com.kai.service.data.request.system.UpdateRoleRequest;
import com.kai.service.data.response.system.RoleResponse;
import com.kai.service.enums.UserStatusEnum;
import com.kai.service.system.service.IRoleResourceService;
import com.kai.service.system.service.IRoleService;
import com.kai.service.util.ResultResponseUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/system")
@Tag(name = "系统角色")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IRoleResourceService roleResourceService;

    /**
     * 获取用户角色
     *
     * @param username
     * @return
     */
    @GetMapping("/role:getByUser")
    @Operation(summary = "获取用户角色")
    public Result<List<RoleResponse>> listUserRole(@RequestParam String username){
        return ResultResponseUtil.success(roleService.listUserRole(username));
    }

    /**
     * 获取所有角色列表
     * @param status
     * @return
     */
    @GetMapping("/role:getList")
    @Operation(summary = "获取所有角色列表")
    public Result<List<RoleResponse>> listRole(@RequestParam("status") String status){
        return ResultResponseUtil.success(roleService.listRole(status));
    }

    /**
     * 修改角色
     *
     * @param roleId
     * @param updateRoleRequest
     * @return
     */
    @PutMapping("/role/{roleId}")
    @Operation(summary = "修改角色")
    public Result<Boolean> updateRole(@PathVariable("roleId") Integer roleId, @RequestBody UpdateRoleRequest updateRoleRequest){
        return ResultResponseUtil.success(roleService.updateRole(roleId, updateRoleRequest));
    }

    /**
     * 添加角色
     *
     * @param createRoleRequest
     * @return
     */
    @PostMapping("/role")
    @Operation(summary = "添加角色")
    public Result<Boolean> createRole(@Valid @RequestBody CreateRoleRequest createRoleRequest){
        return ResultResponseUtil.success(roleService.createRole(createRoleRequest));
    }

    /**
     * 根据资源id获取绑定角色id
     *
     * @param resourceId
     * @return
     */
    @GetMapping("/roleId/{resourceId}")
    @Operation(summary = "根据资源id获取绑定角色id")
    public Result<List<Integer>> getRoleIdsByResourceId(@PathVariable("resourceId") Integer resourceId){
        return ResultResponseUtil.success(roleResourceService.getRoleIdsByResourceId(resourceId));
    }

    /**
     * 启用角色
     *
     * @param roleId
     * @return
     */
    @PutMapping("/role/{roleId}:enable")
    @Operation(summary = "启用角色")
    public Result<Boolean> enableRole(@PathVariable("roleId") Integer roleId){
        return ResultResponseUtil.success(roleService.stateSwitch(roleId, UserStatusEnum.NORMAL.getCode()));
    }

    /**
     * 禁用角色
     *
     * @param roleId
     * @return
     */
    @PutMapping("/role/{roleId}:disabled")
    @Operation(summary = "禁用角色")
    public Result<Boolean> disabledRole(@PathVariable("roleId") Integer roleId){
        return ResultResponseUtil.success(roleService.stateSwitch(roleId, UserStatusEnum.DEACTIVATE.getCode()));
    }
}
